<?php

namespace common\base;

use peng\db\helpers\MysqlHelper;

/**
 * 数据访问对象-基类
 *
 * @author peng.huang
 */
abstract class BaseDao {

    /**
     * 数据库键名
     * 
     * @var string 
     */
    public $dbKey = 'default';

    /**
     * 数据库连接
     * 
     * @var \peng\db\helpers\MysqlHelper 
     */
    public $db;

    /**
     * 表名
     * 
     * @var string 
     */
    public $table = '';

    /**
     * 构造函数
     * 
     * @param string $dbKey [数据库]
     */
    public function __construct($dbKey = NULL) {
        $this->setDB($dbKey);
    }

    /**
     * 设置DB
     * 
     * @param string $dbKey [数据库]
     */
    public function setDB($dbKey) {
        isset($dbKey) AND $this->dbKey = $dbKey;
        $this->db = new MysqlHelper($this->dbKey);
    }

    /**
     * 判断记录是否存在
     * 
     * @param mixed $where [查询条件]
     * @return int
     */
    public function isExists($where) {
        return $this->count($where) > 0;
    }

    /**
     * 统计总数
     * 
     * @param mixed $where [查询条件]
     * @return int
     */
    public function count($where = array()) {
        return $this->db->count($this->table, $where);
    }

    /**
     * 查询一条记录的第一个字段
     * 
     * @param string $fields [字段名称]
     * @param mixed $where [查询条件]
     * @return mixed
     */
    public function scalar($fields, $where = array()) {
        $result = $this->findOne($fields, $where);
        if (!empty($result)) {
            $ar = array_values($result);
            return $ar[0];
        }

        return FALSE;
    }

    /**
     * 查询一条记录
     * 
     * @param string $fields [字段名称,逗号分隔]
     * @param mixed $where [查询条件]
     * @return array
     */
    public function findOne($fields, $where = array()) {
        $result = $this->db->query($fields, $this->table, $where);
        if (!empty($result)) {
            return $result[0];
        }

        return array();
    }

    /**
     * 查询全部记录
     * 
     * @param string $fields [字段名称：多个使用逗号分隔]
     * @param mixed $where [查询条件]
     * @param string $orderBy [排序方式：如id desc]
     * @return array
     */
    public function findAll($fields, $where = array(), $orderBy = NULL) {
        return $this->db->query($fields, $this->table, $where, $orderBy);
    }

    /**
     * 查询分页
     * 
     * @param string $fields [字段名称,逗号分隔]
     * @param array|object $where [查询条件]
     * @param string $orderBy [排序方式,如id desc]
     * @param int $pageIndex [&开始页]
     * @param int $pageSize [每页显示数]
     * @param int $totalRecords [&记录总数]
     * @return array
     */
    public function findPage($fields, $where, $orderBy, &$pageIndex, $pageSize, &$totalRecords) {
        return $this->db->queryPaging($fields, $this->table, $where, $orderBy, $pageIndex, $pageSize, $totalRecords);
    }

    /**
     * 修改记录
     * 
     * @param mixed $entity [实体对象]
     * @param mixed $where [查询条件]
     * @return int
     */
    public function update($entity, $where) {
        return $this->db->update($this->table, $entity, $where);
    }

    /**
     * 保持记录
     * 
     * @param mixed $entity [实体对象]
     * @param string $primaryKey [主键]
     * @return int
     */
    public function save($entity, $primaryKey = 'id') {
        return $this->db->save($this->table, $entity, $primaryKey);
    }

    /**
     * 删除记录
     * 
     * @param mixed $where [查询条件]
     * @return int
     */
    public function delete($where) {
        return $this->db->delete($this->table, $where);
    }

    /**
     * 批量插入
     * 
     * @param string $fields [字段]
     * @param array $values [值]
     * @param int $size [每次插入条数]
     * @return int
     */
    public function batchInsert($fields, $values, $size = 1000) {
        return $this->db->batchInsert($this->table, $fields, $values, $size);
    }

    /**
     * 开始事务
     */
    public function startTrans() {
        return $this->db->driver->startTrans();
    }

    /**
     * 完成事务
     */
    public function completeTrans() {
        return $this->db->driver->completeTrans();
    }

}
